//定义懒加载插件
import { useIntersectionObserver } from '@vueuse/core'

//定义全局指令
export const lazyPlugin = {
  install(app) {
    //懒加载指令逻辑
    app.directive('img-lazy', {
      mounted(el, binding) {
        //el:指令绑定的那个元素
        //binding:binding.value指令等于后面绑定的表达式的值 图片url
        console.log(el, binding.value);
        const { stop } = useIntersectionObserver(
          el,
          ([{ isIntersecting }]) => {
            console.log(isIntersecting);
            if (isIntersecting) {
              //当进入视口区域时，图片发生请求，渲染
              el.src = binding.value
              stop()
            }
          },
        )
      }
    })
  }
}